package com.beep.erp.model.home;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beep.erp.R;
import com.beep.erp.common.bean.BizGoodsListBean;
import com.beep.erp.common.bean.ClassifyBean;
import com.beep.erp.common.bean.StockInDetailBean;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.recyclerview.divider.HorizontalDividerItemDecoration;
import com.beep.erp.model.home.classify.ClassifyActivity;

import java.lang.reflect.Array;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jolly on 2018/9/1.
 */

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.StockDetailViewHolder> {

    //当前上下文对象 需要，，必要参数
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private ArrayList<ClassifyBean.ClassifyListBean> mListBeen;
    private OnItemClickLitener mOnItemClickLitener;
    private boolean isShowGroupItem = false;

    //当前上下文对象mContext必要参数  构造传值
    public HomeAdapter(Context mContext, ArrayList<ClassifyBean.ClassifyListBean> listBeen) {
        this.mContext = mContext;
        this.mListBeen = listBeen;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
        mOnItemClickLitener = onItemClickLitener;
    }

    //初始化布局的地方 new InviteViewHolder 这个是写在下方的 内部类
    @Override
    public StockDetailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new StockDetailViewHolder(mLayoutInflater.inflate(R.layout.group_list_item, parent, false));
    }

    //绑定数据的地方
    @Override
    public void onBindViewHolder(StockDetailViewHolder holder, int position) {
        holder.textView.setText(mListBeen.get(position).getName());
        holder.imageView.setVisibility(View.GONE);

        setRecycleView(holder, position);

        //初始化列表形式
        holder.rlGroup.setVisibility(View.GONE);
        isShowGroupItem = false;

        holder.rlParent.setOnClickListener(view -> {
            if (!isShowGroupItem) {
                holder.rlGroup.setVisibility(View.VISIBLE);
                isShowGroupItem = true;
            } else {
                holder.rlGroup.setVisibility(View.GONE);
                isShowGroupItem = false;
            }
        });

        /*if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(view -> mOnItemClickLitener.onItemClick(view, holder.getAdapterPosition()));
        }*/
    }

    private void setRecycleView(StockDetailViewHolder holder, int position) {
        if (ToolsUtils.isEmptyList(mListBeen.get(position).getChild())) return;
        //创建默认的线性LayoutManager
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        //显示底部位置
        mLayoutManager.setStackFromEnd(true);
        holder.rlGroup.setLayoutManager(mLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        holder.rlGroup.setHasFixedSize(true);
        GroupItemAdapter groupItemAdapter = new GroupItemAdapter(mContext, (ArrayList) mListBeen.get(position).getChild());
        holder.rlGroup.setAdapter(groupItemAdapter);
        groupItemAdapter.setOnItemClickLitener((view, data, position1) -> {
            ClassifyActivity.start(mContext, ((ClassifyBean.ClassifyListBean.ChildBean) data.get(position1)).getId());
        });
    }


    //多少条数据
    @Override
    public int getItemCount() {
        return mListBeen.size();
    }

    //上面提到的内部类 如果要使用@BindView ButterKnife.bind(this, itemView);必须要写
    public class StockDetailViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_title)
        ImageView imageView;
        @BindView(R.id.tv_title)
        TextView textView;
        @BindView(R.id.rl_group)
        RecyclerView rlGroup;
        @BindView(R.id.rl_parent)
        RelativeLayout rlParent;

        public StockDetailViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

}
